<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-24
 * Time: 9:47
 */

namespace app\common\model;


use think\Db;
use think\Model;

class SkillType extends Model
{

    /**
     * 技能类型列表
     */
    public static function skillStypeList($map = [], $page)
    {
           return Db::name('skill_type')->where($map)->paginate(15,false, ['page' => $page]);
    }

    /**
     *添加技能信息
     */
    public static function skillSave($data)
    {
        $data['st_create_time'] = time();
        $data['st_update_time'] = time();
        return Db::name('skill_type')->insert($data);
    }

    /**
     * 技能详情
     */
    public static function detail($id)
    {
        return Db::name('skill_type')->where('st_id', $id)->find();
    }

    /**
     * 修改技能
     */
    public static function updateType($data)
    {
        return Db::name('skill_type')->update($data);
    }

    /**
     * 禁用启用
     */
    public static function editStatus($id, $status)
    {
        return Db::name('skill_type')->where('st_id', $id)->update(['st_status' => $status, 'st_update_time' => time()]);
    }

    /**
     * 删除
     */
    public static function del($id)
    {
        return Db::name('skill_type')->where('st_id', $id)->delete();
    }

    /**
     * PC前台根据类型的状态获取该类型下面的值
     */
    public static function getAllInfoForStatus($st_type)
    {
        $data = Db::name('skill_type')->where('st_type', $st_type)
            ->where('st_status', 1)
            ->field('*')->select();
        if (sizeof($data) > 0) {
            foreach($data as $k => $v) {
                if ($v['st_photo_just'] != '') {
                    $data[$k]['st_photo_just'] = 'http://'.$_SERVER['HTTP_HOST'].$v['st_photo_just'];
                }
                if ($v['st_photo_back'] != '') {
                    $data[$k]['st_photo_back'] = 'http://'.$_SERVER['HTTP_HOST'].$v['st_photo_back'];
                }
            }
            return $data;
        } else {
            return false;
        }

    }

    /**
     * 统计平台发布最多的前十种项目类型
     */
    public static function getTopTenType()
    {
        $application_type = Db::name('project')
            ->group('p_application_type')
            ->order('count(p_id) desc')
            ->limit(10)
            ->field('p_application_type as id, count(p_id) as count')
            ->select();
        if (sizeof($application_type) > 0) {
            foreach($application_type as $k=>$v){
                $data = Db::name('skill_type')->where('st_id', $v['id'])
                    ->field('st_name, st_photo_just')->find();
                $application_type[$k]['name'] = $data['st_name'];
                $application_type[$k]['photo'] = 'http://'.$_SERVER['HTTP_HOST'].$data['st_photo_just'];
            }
            return $application_type;
        } else {
            return false;
        }
    }
}